
//
class InstallController {

    def index = {
		render(view:'install')
	}

	def clearAll = {
		def exists = Privilege.findAll()
		for(Privilege p in exists){
			p.delete(flush:true)
		}

		exists = Role.findAll()
		for(Role r in exists){
			r.delete(flush:true)
		}
	}

	// Install system with data initialized
	def installDefault = {
		log.info 'Start to install default data'
		initPrivileges()
		initRoles()
		initSystemUser()
		flash.message = 'System data created'
		redirect(action:'index')
	}

	def installDemo = {
		log.info 'Start to install demo data...'
		initQuestions()
		flash.message = 'Demo data installed.'
		redirect(action: 'index')
	}

	// Init for system default privileges.
	def initPrivileges(){
		// USER
		saveEntity( new Privilege(privName:'SHOW_USER',description:''))
		saveEntity( new Privilege(privName:'CREATE_USER',description:''))
		saveEntity( new Privilege(privName:'UPDATE_USER',description:''))
		saveEntity( new Privilege(privName:'DELETE_USER',description:''))
		// ROLE
		saveEntity( new Privilege(privName:'SHOW_ROLE',description:''))
		saveEntity( new Privilege(privName:'CREATE_ROLE',description:''))
		saveEntity( new Privilege(privName:'UPDATE_ROLE',description:''))
		saveEntity( new Privilege(privName:'DELETE_ROLE',description:''))
		// ROLE
		saveEntity( new Privilege(privName:'SHOW_PRIVILEGE',description:''))
		saveEntity( new Privilege(privName:'CREATE_PRIVILEGE',description:''))
		saveEntity( new Privilege(privName:'UPDATE_PRIVILEGE',description:''))
		saveEntity( new Privilege(privName:'DELETE_PRIVILEGE',description:''))
		//
		saveEntity( new Privilege(privName:'SYSTEM_SETTING ',description:''))
	}

	// Init for system default Roles
	def initRoles() {
		def privileges = Privilege.findAll()
		if(!Role.findByRoleName('Admin')){
			saveEntity(new Role(roleName:'Admin', description:'Administrator', privileges:privileges))
		}
		if(!Role.findByRoleName('Guest')){
			saveEntity( new Role(roleName:'Guest', description:'Guest', department:'Guest', privileges: null))
		}
	}

	def initSystemUser() {
		def u1 = new User(userName:'admin', password:'admin@grails-ext', firstName:'Administrator', department:'系统管理员', creationTime: Calendar.getInstance())
		def u2 = new User(userName:'zhangsan', password:'asdfasdf', firstName:'张三', department:'信息部', creationTime: Calendar.getInstance())
		def u3 = new User(userName:'lisi', password:'asdfasdf', firstName:'李四', department:'信息部', creationTime: Calendar.getInstance())
		def u4 = new User(userName:'wangwu', password:'asdfasdf', firstName:'王五', department:'信息部', creationTime: Calendar.getInstance())
		def r = Role.findByRoleName('Admin')
		println "role: ${r}"
		if(r){
			u1.roles = [r]
		}
		def gr = Role.findByRoleName("Guest")
		if(gr) {
			u2.roles = [gr]
			u3.roles = [gr]
			u4.roles = [gr]
		}
		saveEntity(u1)
		saveEntity(u2)
		saveEntity(u3)
		saveEntity(u4)
	}

	def initQuestions(){
		def qi1 = new QuestionItem(quesItemId: 'A', content: '巴西')
		def qi2 = new QuestionItem(quesItemId: 'B', content: '阿根廷')
		def qi3 = new QuestionItem(quesItemId: 'C', content: '英格兰')
		def qi4 = new QuestionItem(quesItemId: 'D', content: '德国')
		def qi5 = new QuestionItem(quesItemId: 'E', content: '法国')
		def question1 = new Question(content: '你认为本届世界杯冠亚军是谁？', type:'multi_select', multiSelect: 3)
		question1.addToQuestionItems(qi1)
		question1.addToQuestionItems(qi2)
		question1.addToQuestionItems(qi3)
		question1.addToQuestionItems(qi4)
		question1.addToQuestionItems(qi5)

		def qi2_1 = new QuestionItem(quesItemId: 'A', content: '100年后')
		def qi2_2 = new QuestionItem(quesItemId: 'B', content: '200年后')
		def qi2_3 = new QuestionItem(quesItemId: 'C', content: '永远不可能了')
		def question2 = new Question(content: '你认为中国男足何时能够打入世界杯决赛？', type:'multi_select', multiSelect: 2)
		question2.addToQuestionItems(qi2_1)
		question2.addToQuestionItems(qi2_2)
		question2.addToQuestionItems(qi2_3)

		def question3 = new Question(content: '请简述中国男足和猪的共同之处？', type:'answer', multiSelect: 1)
		def question4 = new Question(content: '你怎么看待中国足协这个机构？', type:'answer', multiSelect: 1)

		def qstire = new Questionnaire(qstireName: '调查问卷', questionNum: 4)
		qstire.addToQuestions(question1)
		qstire.addToQuestions(question2)
		qstire.addToQuestions(question3)
		qstire.addToQuestions(question4)
		saveEntity(qstire)
	}

	def boolean saveEntity(entity){
		if(entity.hasErrors()){
			println "Entity ${entity} has errors"
			entity.errors.each{
				println it
			}
			flash.message = entity.errors
			return false
		} else {
			if(!entity.save(flush:true)){
				println "Save entity ${entity} with errors"
				entity.errors.each{
					println it
					flash.message = entity.errors
				}
				return false
			}
			return true
		}
	}

}
